package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * packageName com.sky.service.impl
 *
 * @author fenglai
 * @version JDK 11
 * @className DishServiceImpl
 * @date 2023/12/10
 * @description TODO
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult pageSelectDish(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        List<Dish> dish1 = dishMapper.getDish(Dish.builder().name(dto.getName()).categoryId(dto.getCategoryId()).status(dto.getStatus()).build());

       List<DishVO> dishVOS = new Page<>();

        for (Dish dish2 : dish1) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish2, dishVO);
            List<Category> category = categoryMapper.getCategory(Category.builder().id(dish2.getCategoryId()).build());
            dishVO.setCategoryName(category.get(0).getName());
            dishVOS.add(dishVO);
        }
        Page p = (Page) dishVOS;

        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public List<Dish> selectDishByCategoryId(Long categoryId) {

        return dishMapper.getDish(Dish.builder().categoryId(categoryId).build());

    }

    @Override
    public DishVO selectDishById(Long id) {

        List<Dish> dish = dishMapper.getDish(Dish.builder().id(id).build());
        List<DishFlavor> list = dishFlavorMapper.getFlavor(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish.get(0), dishVO);
        List<Category> category = categoryMapper.getCategory(Category.builder().id(dishVO.getCategoryId()).build());
        dishVO.setCategoryName(category.get(0).getName());
        if (!list.isEmpty()) {
            dishVO.setFlavors(list);
        }
        return dishVO;
    }

    @Override
    public void addDish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.addDish(dish);
        dishFlavorMapper.deleteDishFlavorByDishId(dish.getId());
        List<DishFlavor> flavors = dto.getFlavors();
        if (!flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public void updateDish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.updateDish(dish);
        dishFlavorMapper.deleteDishFlavorByDishId(dish.getId());
        List<DishFlavor> flavors = dto.getFlavors();
        if (!flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public void updateDishStatus(Integer status, Long id) {

        dishMapper.updateDish(Dish.builder().status(status).id(id).build());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDish(List<Long> ids) {

        for (Long id : ids) {
            List<Dish> dish = dishMapper.getDish(Dish.builder().id(id).build());
            if (dish.get(0).getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            List<SetmealDish> list = setmealDishMapper.getSetmealDishByDishId(id);
            if (list.size() > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            dishFlavorMapper.deleteDishFlavorByDishId(id);
        }
        dishMapper.deleteDish(ids);

    }


}